package com.gdk.ksoft.assembly.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import java.util.List;

/**
 * Created by Mrper on 2014/11/22.
 * 页面菜单
 */
public class OperatorMenu extends PopupWindow {

    @ViewInject(R.id.menu_operator_lstOperator)private ListView lstOperator;

    private Context context;
    private List<OperatorItem> items;//操作项
    private AdapterView.OnItemClickListener listener;//列表项点击事件

    public OperatorMenu(Context context,List<OperatorItem> items){
        super();
        this.context = context;
        this.items = items;
        View rootView = View.inflate(context, R.layout.menu_topright_operator, null);
        ViewUtils.inject(this,rootView);
        lstOperator.setAdapter(adapter);
        setContentView(rootView);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setFocusable(true);
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        update();//更新窗口
        ViewUtils.inject(this, rootView);

    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
        if(listener!=null){
            this.listener = listener;
            lstOperator.setOnItemClickListener(listener);
        }
    }

    public AdapterView.OnItemClickListener getOnItemClickListener(){
        return this.listener;
    }

    /**
     * 列表数据类
     */
    public static class OperatorItem{

        public OperatorItem(int iconId,String itemText){
            this.iconId = iconId;
            this.itemText = itemText;
        }

        /**
         * 列表图标
         */
        public int iconId = 0;
        /**
         * 列表文字
         */
        public String itemText;
    }

    /**
     * 菜单数据适配器
     */
    public BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return items==null?0:items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final OperatorItem item = items.get(position);
            convertView = View.inflate(context,R.layout.item_menu_operator,null);
            ImageView imgIcon = (ImageView)convertView.findViewById(R.id.item_menu_operator_imgIcon);
            TextView txtItemText = (TextView)convertView.findViewById(R.id.item_menu_operator_txtItemText);
            imgIcon.setImageResource(item.iconId);
            txtItemText.setText(item.itemText);
            return convertView;
        }
    };



}
